{
    //地图类
    class Map {
        constructor() {
            let canvas = document.getElementById("myCanvas");
            this._ctx = canvas.getContext("2d");

            //一节身体的大小
            this.bodySize = {
                width: 4,
                height: 4
            }

            //单位像素大小
            this.unitSize = 4;

            // _ctx.beginPath();
            // _ctx.rect(20,20,4,4);
            // _ctx.fillStyle="#fff";
            // _ctx.fill();

            //宽度
            this.WIDTH = 800;
            //高度
            this.HEIGHT = 600;
            //蛇组
            this.snakes = [];
            //食物组
            this.foods = [];

            //对蛇组添加事件监听
            this._listenKeyDown();
        }

        //运行游戏
        run(frames) {
            //启动蛇运动
            this._beginMove();

            return setInterval(() => {
                //刷新画布
                this._draw();
                //随机产生食物
                this._produceFood();
            }, 1000/frames);

        }

        //添加条蛇
        addSnakes(snakeObj) {
            //如果是数组
            if ( snakeObj.constructor === Array ) {
                let snakes = snakeObj;
                this.snakes = this.snakes.concat(snakes);
                snakes.forEach(element => {
                    if ( element.snake ) {
                        element.snake.setInitCoor(
                            new Coordinate(
                                parseInt(utils.random(20, this.WIDTH - 20) / this.unitSize) * this.unitSize,
                                parseInt(utils.random(20, this.HEIGHT - 20) / this.unitSize) * this.unitSize
                            )
                        );
                        element.snake.unitSize = this.unitSize;

                        console.log(element.snake.getBodyCoors());
                    }
                });
            }

            //如果是对象
            if ( snakeObj.constructor === Object ) {
                snakeObj.snake.setInitCoor(
                    new Coordinate(
                        parseInt(utils.random(20, this.WIDTH - 20) / this.unitSize) * this.unitSize,
                        parseInt(utils.random(20, this.HEIGHT- 20) / this.unitSize) * this.unitSize
                    )
                )
                snakeObj.unitSize = this.unitSize;
                this.snakes.push(snakeObj);
            }

            
        }

        //生产食物
        _produceFood() {
            if ( this.foods.length === 0 ) {
                this.foods = [
                    new Food({
                        coor: {
                            x: parseInt(utils.random(40, this.WIDTH - 40) / this.unitSize) * this.unitSize,
                            y: parseInt(utils.random(40, this.HEIGHT - 40) / this.unitSize) * this.unitSize
                        },
                        color: "red"
                    }),
                    new Food({
                        coor: {
                            x: parseInt(utils.random(40, this.WIDTH - 40) / this.unitSize) * this.unitSize,
                            y: parseInt(utils.random(40, this.HEIGHT - 40) / this.unitSize) * this.unitSize
                        },
                        color: "red"
                    })
                ]
            }
        }

        //移动蛇
        _beginMove() {
            setInterval(() => {
                this.snakes.forEach((snakeObj, index) => {
                    switch(snakeObj.snake.orientation) {
                        //上
                        case "up":
                            snakeObj.snake.moveUp();
                        break;
                        //下
                        case "down":
                            snakeObj.snake.moveDown();
                        break;
                        //左
                        case "left":
                            snakeObj.snake.moveLeft();
                        break;
                        //右
                        case "right":
                            snakeObj.snake.moveRight();
                        break;
                    }
                    //碰撞检测
                    this._collisionDetection(snakeObj.snake, index);
                });
            }, 200);
        }

        //碰撞检测
        _collisionDetection(snake, snakeIndex) {
            //检测蛇与食物是否碰撞
            this.foods.forEach((food, index) => {

                let bodyCoors = snake.getBodyCoors();
                for(let i = 0; i <= bodyCoors.length - 1; i++ ) {
                    let cRes = this._rectCollision({
                        x: bodyCoors[i].x,
                        y: bodyCoors[i].y,
                        width: this.bodySize.width,
                        height: this.bodySize.height
                    },{
                        x: food.coor.x,
                        y: food.coor.y,
                        width: this.bodySize.width,
                        height: this.bodySize.height
                    });

                    //如果产生碰撞
                    if ( cRes ) {
                        //让蛇吃掉这个食物
                        snake.eat({x:food.coor.x, y:food.coor.y});
                        //从食物数组中移除这个食物
                        this.foods.splice(index, 1);
                        //跳出循环
                        break;
                    }
                }
            });

            //检测当前蛇是否与其他蛇产生碰撞
            this.snakes.forEach(snakeObj => {
                if (snake.snakeId !== snakeObj.snake.snakeId ) {
                    console.log(11);
                    let bodyCoors = snake.getBodyCoors();
                    let _bodyCoors = snakeObj.snake.getBodyCoors();
                    //去掉别的蛇的头部
                    _bodyCoors.shift();
                    //得到当前蛇的头部
                    let head = {
                        x: bodyCoors[0].x,
                        y: bodyCoors[0].y
                    }
                    for (let i = 0; i <= _bodyCoors.length - 1; i++ ) {
                        let cRes = this._rectCollision({
                            x: head.x,
                            y: head.y,
                            width: this.bodySize.width,
                            height: this.bodySize.height
                        },{
                            x: _bodyCoors[i].x,
                            y: _bodyCoors[i].y,
                            width: this.bodySize.width,
                            height: this.bodySize.height
                        });

                        //如果产生碰撞
                        if ( cRes ) {
                            console.log(snake.snakeId, " 产生碰撞 ", "，宣布死亡！");
                            //让蛇死亡
                            snake.die();
                            //从蛇组中移除
                            this.snakes.splice(snakeIndex, 1);
                            //跳出循环
                            break;
                        }

                    }
                }
            });


        }

        //矩形碰撞检测
        _rectCollision(rect1, rect2) {
            if ( rect1.x < rect2.x + rect2.width &&
                rect1.x + rect1.width > rect2.x &&
                rect1.y < rect2.y + rect2.height &&
                rect1.height + rect1.y > rect2.y ) {
                return true;
            } else {
                return false;
            }
        }

        //清空画布
        _clearCanvas() {
            this._ctx.clearRect(0, 0, this.WIDTH, this.WIDTH);
        }

        //刷新画布
        _draw() {
            //先清空画布
            this._clearCanvas();
            //开始画蛇
            this.snakes.forEach(snakeObj => {
                //获取蛇的信息
                let coors = snakeObj.snake.getBodyCoors();
                let bodyColor = snakeObj.snake.getBodyColor();
                //画出蛇的所有身体
                coors.forEach(coor => {
                    this._drawPoint(coor, bodyColor, this.bodySize);
                });
            });

            //开始画食物
            this.foods.forEach(food => {
                this._drawPoint(food.coor, food.color, this.bodySize);
            });
        }

        //画一个单位点
        _drawPoint(coors, color, size) {
            this._ctx.beginPath();
            this._ctx.rect(coors.x, coors.y, size.width, size.height);
            this._ctx.fillStyle = color;
            this._ctx.fill();
        }

        //监听键盘事件
        _listenKeyDown() {
            window.document.onkeydown = (e) => {//对整个页面文档监听 
                let keyCode = window.event ? e.keyCode :e.which;  //获取被按下的键值
                keyCode = keyCode.toString();
                this.snakes.forEach(snakeObj => {
                    switch(keyCode) {
                        //上
                        case snakeObj.up:
                            snakeObj.snake.orientation = "up";
                            snakeObj.snake.moveUp();
                        break;
                        //下
                        case snakeObj.down:
                            snakeObj.snake.orientation = "down";
                            snakeObj.snake.moveDown();
                        break;
                        //左
                        case snakeObj.left:
                            snakeObj.snake.orientation = "left";
                            snakeObj.snake.moveLeft();
                        break;
                        //右
                        case snakeObj.right:
                            snakeObj.snake.orientation = "right";
                            snakeObj.snake.moveRight();
                        break;
                    }
                });
            }
        }
    }

    window.Map = Map;
}